<?php

global $_W, $_GPC;
$_GPC['do'] = 'type';
$_GPC['menu'] = 'question';

if ($_GPC['act'] == 'mutdelete') {
    $ids = $_GPC['ids'];
    foreach ($ids as $id) {
        M('type')->delete($id);
    }
    message('删除成功', referer(), 'success');
}
if ($_GPC['act'] == 'edit') {
    $id = intval($_GPC['id']);
    if ($_W['ispost']) {
        $data = array();
        $data['uniacid'] = $_W['uniacid'];
        if (isset($_GPC['name'])) {
            $data['name'] = trim($_GPC['name']);
        }
        if (!empty($_GPC['answer_type'])) {
            $data['answer_type'] = trim($_GPC['answer_type']);
        }

        if (isset($_GPC['sort'])) {
            $data['sort'] = intval($_GPC['sort']);
        }

        $data['status'] = intval($_GPC['status']);
        if (!empty($id)) {
            $data['id'] = $id;
            $question = M('type')->update($data);
        } else {
            $question = M('type')->update($data);
        }
        message('保存成功', $this->createWebUrl('type'), 'success');
    }
    $item = M('type')->getInfo($id);
    include $this->template('type_edit');
    exit;
}

if ($_GPC['act'] == 'reset') {
    $type = M('type')->getall();
    $haveData = false;
    if (!empty($isSet)) {
        foreach ($type as $key => $value) {
            // 判断是否存在问题
        }
        if ($haveData) {
            message('分类下有数据，请先清空问题数据', $this->createWebUrl('type'), 'error');
            exit;
        }
    }
    M('type')->deleteByParams();
//    $resetData = array(
//        array('历届校友',MODULE_URL.'assets/images/type/type_alumni.png', 8),
//        array('怀旧照片',MODULE_URL.'assets/images/type/type_picture.png', 7),
//        array('知名校友',MODULE_URL.'assets/images/type/type_famous.png', 6),
//        array('校友荣誉',MODULE_URL.'assets/images/type/type_honor.png', 5),
//        array('校友祝福',MODULE_URL.'assets/images/type/type_bless.png', 4),
//        array('学校故事',MODULE_URL.'assets/images/type/type_story.png', 3),
//        array('校园活动',MODULE_URL.'assets/images/type/type_activity.png', 2),
//        array('校园新闻',MODULE_URL.'assets/images/type/type_news.png', 1),
//    );
//    foreach ($resetData as $key => $value) {
//        $item = array(
//            'name'          => $value[0],
//            'image'         => $value[1],
//            'status'        => 1,
//            'sort'          => $value[2],
//            'create_time'   => time()
//        );
//        M('type')->update($item);
//    }
    message('重置成功', $this->createWebUrl('type'), 'success');
}

if ($_GPC['act'] == 'delete') {
    $id = intval($_GPC['id']);
    if (empty($id)) {
        if ($_W['ispost']) {
            $data = array();
            $data['status'] = 1;
            $data['message'] = '参数错误';
            die(json_encode($data));
        } else {
            message('参数错误', referer(), 'error');
        }
    }
    M('type')->delete($id);
    if ($_W['ispost']) {
        $data = array();
        $data['status'] = 1;
        $data['message'] = '操作成功';
        die(json_encode($data));
    } else {
        message('删除成功', referer(), 'success');
    }
}

$page = !empty($_GPC['page']) ? intval($_GPC['page']) : 1;
$where = "";
$list = M('type')->getList($page, $where);

if (!empty($list['list'])) {
    $answerType = array(
        1 => '单选',
        2 => '多选',
        3 => '文本'
    );
    foreach ($list['list'] as $key => $value) {
        $list['list'][$key]['status'] = ($value['status'] == 1) ? '显示' : '隐藏';
        $list['list'][$key]['answer_type'] = $answerType[$value['answer_type']];
    }
}

include $this->template('type');